<?php
//读者类
class member extends mysql
{
	private $tablename;
	private $type;
	private $borrow;
	private $admin;
	private $admintype;
	
	public function __construct()
	{
		parent::__construct();
		$this->tablename = PREFIX . 'reader';
		$this->type = PREFIX . 'rtype';
		$this->borrow = PREFIX . 'borrow';
		$this->admin = PREFIX . 'admin';
		$this->admintype = PREFIX . 'utype';
	}
	
   /**
	*函数: reader_class()
	*功能: 读取所有读者分类
	*参数: 无
	*返回: 失败返回false
	*/
	public function reader_class()
	{
		$sql = "select * from `{$this->type}`";
		if($res = $this->_selects($sql))
		{
			return $res;
		}
		else
		{
			return false;
		}
	}
	
   /**
	*函数: up_member_class()
	*功能: 编辑读者分类
	*参数: 无
	*返回: 失败返回false
	*/
	public function up_member_class($arr)
	{
		$count = 0;
		array_pop($arr);
		foreach($arr as $val)
		{
			if($val[1] == '' || !preg_match('/^\d+$/', $val[2]))
			{
				$this->_message('member_main.php?action=class', '请检查必填项，最大借书量只能为数字！');
			}
			$sql = sprintf("update `{$this->type}` set `name`='%s', `max_borrow`=%d where `tid`=%d", $val[1], $val[2], $val[0]);
			if($this->_update($sql))
			{
				$count++;
			}
		}
		if($count)
		{
			$this->_message('member_main.php?action=class', '修改成功！');
		}
		else
		{
			$this->_message('member_main.php?action=class', '未修改任何内容！');
		}
	}
	
   /**
	*函数: _read_member()
	*功能: 读取所有读者
	*参数: 无
	*返回: 失败返回false
	*/
	public function _read_member()
	{
		$sql = "select * from `{$this->tablename}` left join `{$this->type}` on `{$this->tablename}`.`type` = `{$this->type}`.`typeid`";
		if($res = $this->_selects($sql))
		{
			$num = count($res);
			for($i = 0; $i <$num; $i++)
			{
				$res[$i]['borrow'] = $res[$i]['total'];		//借书的数量
				//$res[$i]['borrow'] = count($this->_read_member_borrow($res[$i]['id']));		//借的书数量
				$res[$i]['lasttime'] = d($res[$i]['lasttime']);
			}
			return $res;
		}
		else
		{
			return false;
		}
	}
	
   /**
	*函数: _read_a_member($id)
	*功能: 读取一个读者
	*参数: $id
	*返回: 失败返回false
	*/
	public function _read_a_member($id)
	{
		$sql = sprintf("select * from `{$this->tablename}` left join `{$this->type}` on `{$this->tablename}`.`type` = `{$this->type}`.`typeid` where `id`=%d", $id);
		if($res = $this->_select($sql))
		{
			return $res;
		}
		else
		{
			return false;
		}
	}
	
   /**
	*函数: _edit_member($post)
	*功能: 编辑读者信息
	*参数: $post
	*返回: 失败返回false
	*/
	public function _edit_member($in)
	{
		foreach($in as $v)
		{
			if($v == '')
			{
				$this->_message('member_main.php', '姓名或email不能为空');
			}
		}
		$query = sprintf("update `{$this->tablename}` set 
					 	 `username`='%s',
						 `type`=%d,
						 `sex`='%s',
						 `email`='%s' 
						 where `id`=%d",
						 mysql_real_escape_string($in['username']),
						 mysql_real_escape_string($in['typeid']),
						 mysql_real_escape_string($in['sex']),
						 mysql_real_escape_string($in['email']),
						 $in['id']);
		if($this->_update($query))
		{
			$this->_message('member_main.php', '修改成功！');
		}
		else
		{
			return false;
		}
	}

   /**
	*函数: _del_member($id)
	*功能: 删除读者，如果读者有借书则删除失败
	*参数: $id
	*返回: 失败返回false
	*/	
	public function _del_member($id)
	{
		$query = sprintf("select `total` from `{$this->tablename}` where `id`=%d", $id);
		if($res = $this->_select($query))
		{
			if($res['total'] !== 0)
			{
				$this->_message('member_main.php', '删除失败！此用户还有未归还的图书！');
			}
		}
		$sql = sprintf("delete from `{$this->tablename}` where `id`=%d", $id);
		if($this->_delete($sql))
		{
			$this->_message('member_main.php', '删除成功！');
		}
		else
		{
			$this->_message('member_main.php', '删除失败！');
		}
	}
	
//----------------------type

   /**
	*函数: _read_type()
	*功能: 读取所有分类
	*参数: 无
	*返回: 失败返回false
	*/
	public function _read_type()
	{
		$sql = "select * from `{$this->type}` where 1";
		if($res = $this->_selects($sql))
		{
			return $res;
		}
	}
//----------------------borrow

   /**
	*函数: _read_member_borrow($id)
	*功能: 读取读者借书
	*参数: $id
	*返回: 失败返回false
	*/
	public function _read_member_borrow($id)
	{
		$sql = sprintf("select * from `{$this->borrow}` where `readerid`=%d", $id);
		if($res = $this->_selects($sql))
		{
			return $res;
		}
		else
		{
			return false;
		}
	}
//-----------------------------管理员---------------
   /**
	*函数: _read_admin()
	*功能: 读取所有管理员
	*参数: 无
	*返回: 失败返回false
	*/
	public function _read_admin()
	{
		$query = "select * from `{$this->admin}` left join `{$this->admintype}` on `{$this->admin}`.`type`=`{$this->admintype}`.`typeid`";
		if($res = $this->_selects($query))
		{
			$num = count($res);
			for($i = 0; $i <$num; $i++)
			{
				$res[$i]['lasttime'] = d($res[$i]['lasttime']);
			}
			return $res;
		}
		else
		{
			return false;
		}
	}
	
   /**
	*函数: _read_admintype($id)
	*功能: 读取所有管理员分类
	*参数: 无
	*返回: 失败返回false
	*/
	public function _read_admintype()
	{
		$sql = sprintf("select * from `{$this->admintype}` where 1");
		if($res = $this->_selects($sql))
		{
			return $res;
		}
		else
		{
			return false;
		}
	}

   /**
	*函数: _read_a_admin($id)
	*功能: 读取一个管理员
	*参数: 无
	*返回: 失败返回false
	*/
	public function _read_a_admin($id)
	{
		$sql = sprintf("select * from `{$this->admin}` left join `{$this->admintype}` on `{$this->admin}`.`type` = `{$this->admintype}`.`typeid` where `id`=%d", $id);
		if($res = $this->_select($sql))
		{
			return $res;
		}
		else
		{
			return false;
		}	
	}
	
   /**
	*函数: _edit_admin($id)
	*功能: 读取一个管理员
	*参数: 无
	*返回: 失败返回false
	*/
	public function _edit_admin($in)
	{
		foreach($in as $v)
		{
			if($v == '')
			{
				$this->_message('admin_main.php', '姓名或email不能为空');
			}
		}
		if($in['id'] == 1)
		{
			$this->_message('admin_main.php', '非法动作！禁止！');
		}
		$query = sprintf("update `{$this->admin}` set 
					 	 `username`='%s',
						 `type`=%d,
						 `sex`='%s',
						 `email`='%s' 
						 where `id`=%d",
						 mysql_real_escape_string($in['username']),
						 mysql_real_escape_string($in['typeid']),
						 mysql_real_escape_string($in['sex']),
						 mysql_real_escape_string($in['email']),
						 $in['id']);
		if($this->_update($query))
		{
			$this->_message('admin_main.php', '修改成功！');
		}
		else
		{
			return false;
		}
	}
	
   /**
	*函数: _add_admin()
	*功能: 增加一个管理员
	*参数: 无
	*返回: 失败返回false
	*/
	public function _add_admin($arr)
	{
		$sql = sprintf("select * from `{$this->admin}` where `userid`='%s'", mysql_real_escape_string($arr['userid']));
		if($res = $this->_select($sql))
		{
			$this->_message('admin_main.php?action=add', '用户名已存在！');
		}
		$query = sprintf("insert into `{$this->admin}` 
				 (`id`, `userid`, `username`, `password`, `email`, `sex`, `lasttime`, `lastip`,`type`)
				 values('', '%s', '%s', sha1('%s'), '%s', '%s', %d, '%s', %d)",
				 mysql_real_escape_string($arr['userid']),
				 mysql_real_escape_string($arr['username']),
				 mysql_real_escape_string($arr['pwd2']),
				 mysql_real_escape_string($arr['email']),
				 mysql_real_escape_string($arr['sex']),
				 time(),
				 $_SERVER['REMOTE_ADDR'],
				 mysql_real_escape_string($arr['type'])
				 );
		if($this->_insert($query))
		{
			$this->_message('admin_main.php', '添加管理员成功！');
		}
	}
	
   /**
	*函数: _del_admin($id)
	*功能: 删除一个管理员
	*参数: 无
	*返回: 失败返回false
	*/
	public function _del_admin($id)
	{
		$sql = sprintf("delete from `{$this->admin}` where `id`=%d", $id);
		if($this->_delete($sql))
		{
			$this->_message('admin_main.php', '删除成功！');
		}
	}
	
   /**
	*函数: _read_member_search($kw)
	*功能: 按分类搜索
	*参数: 无
	*返回: 失败返回false
	*/
	public function _read_member_search($kw)
	{
		$kw = htmlspecialchars(mysql_real_escape_string($kw));
		if(empty($kw))
		{
			$this->_message('member_main.php', '请输入查询的关键字！');
		}
		$sql  = sprintf("select * from `{$this->tablename}` left join `{$this->type}`
						on `{$this->tablename}`.`type` = `{$this->type}`.`typeid`  where `{$this->type}`.`name` like '%%%s%%'", $kw);
		if($res = $this->_selects($sql))
		{
			foreach($res as $k=>$v)
			{
				$res[$k]['name'] = preg_replace("/($kw)/", "<font color='red'>\\1</font>", $v['name']);
				$sql1 = "select * from ``";
			}
			return $res;
		}
		else
		{
			return false;
		}
	}

   /**
	*函数: _add_member($arr)
	*功能: 添加用户
	*参数: 无
	*返回: 失败返回false
	*/
	public function _add_member($arr)
	{
		$sql = sprintf("select * from `{$this->tablename}` where `userid`='%s'", mysql_real_escape_string($arr['userid']));
		if($res = $this->_select($sql))
		{
			$this->_message('member_main.php?action=add', '用户名已存在！');
		}
		$query = sprintf("insert into `{$this->tablename}` 
				 (`id`, `userid`, `username`, `password`, `email`, `sex`, `lasttime`, `lastip`,`type`, `total`, `collect`)
				 values('', '%s', '%s', sha1('%s'), '%s', '%s', %d, '%s', %d, 0, '')",
				 mysql_real_escape_string($arr['userid']),
				 mysql_real_escape_string($arr['username']),
				 mysql_real_escape_string($arr['pwd2']),
				 mysql_real_escape_string($arr['email']),
				 mysql_real_escape_string($arr['sex']),
				 time(),
				 $_SERVER['REMOTE_ADDR'],
				 mysql_real_escape_string($arr['type'])
				 );
		if($this->_insert($query))
		{
			$this->_message('member_main.php', '添加读者成功！');
		}
	}
		
   /**
	*函数: tongji_reader()
	*功能: 统计用户信息
	*参数: 无
	*返回: 失败返回false
	*/
	public function tongji_reader()
	{
		$res = $this->_read_type();		//读取所有等级
		foreach($res as $k=>$v)
		{
			$typeid = $v['typeid'];
			$sql  = "select `id` from `{$this->tablename}` where `type`={$typeid}";
			$rows[$v['name']] = $this->_num_rows($sql);
		}
		return $rows;		//返回各级读者的树木
	}
//--------------------------------------------class end --------------------------------------------
}
?>